﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ContactController
/// </summary>
public class ContactController:Interface
{
    private SqlConnection con;
	public ContactController()
	{
	    con = ConnectDB.Connect();
        ConnectDB.Open();
	}

    public int Insert(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Insert_Contact";
            cmd.CommandType = CommandType.StoredProcedure;
            Contact contact = (Contact)obj;
            cmd.Parameters.Add("@name", SqlDbType.NText).Value = contact.Name;
            cmd.Parameters.Add("@email", SqlDbType.NText).Value = contact.Email;
            cmd.Parameters.Add("@phone", SqlDbType.VarChar, 50).Value = contact.Phone;
            cmd.Parameters.Add("@title", SqlDbType.NText).Value = contact.Title;
            cmd.Parameters.Add("@content", SqlDbType.NText).Value = contact.Content;
            cmd.Parameters.Add("@dateStart", SqlDbType.DateTime).Value = contact.DateStart;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Update(object obj)
    {
        return 0;
    }

    public int Delete(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Delete_Contact";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@contact_id", SqlDbType.Int).Value = id;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
        
    }

    public DataTable GetAll()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Contact ORDER BY dateStart DESC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {
            return null;
            throw;
        }
    }

    public DataTable GetByID(int id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Contact WHERE contact_id=@id";
            cmd.Parameters.AddWithValue("@id", id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {
            return null;
            throw;
        }
    }
    public List<Contact> GetListContactAll()
    {
        List<Contact> list = new List<Contact>();
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Contact ORDER BY dateStart DESC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            foreach (DataRow item in dt.Rows)
            {
                Contact c = new Contact(item);
                list.Add(c);
            }
        }
        catch (Exception)
        {
            throw;
        }
        return list;
    }
}